﻿package net.hcpme.activity;

import java.util.ArrayList;

import net.hcpme.dao.Message;
import net.hcpme.dao.MessageDao;
import net.hcpme.service.ReceiveMessageService;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class ChatRoom extends BaseActivity {

    public static final int MESSAGE_TYPE_IN = 0;// 接收消息
    public static final int MESSAGE_TYPE_OUT = 1;// 发送消息

    // ui
    private ListView chatLv;
    private Button sendBtn;
    private EditText messageEt;

    private MessageDao messageDao;

//    private ReceiveMessageService connectService;
//
//    // 连接对象
//    private ServiceConnection conn = new ServiceConnection() {
//        @Override
//        public void onServiceConnected(ComponentName name, IBinder iBinder) {
//            Log.d("ChatRoom", "连接成功！");
//            LocalBinder localBinder = (LocalBinder) iBinder;
//            connectService = localBinder.getService();
//        }
//
//        @Override
//        public void onServiceDisconnected(ComponentName arg0) {
//            Log.i("ChatRoom", "断开连接！");
//            connectService = null;
//        }
//    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_room);

        initView();

        // 创建Intent
        Intent intent = new Intent();
        // 设置Class属性
        intent.setClass(this, ReceiveMessageService.class);
        // bindService(intent, conn, Service.BIND_AUTO_CREATE);

        messageDao = new MessageDao(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        messageDao.releaseHelper();
        // this.unbindService(conn);
    }

    private void initView() {
        chatLv = (ListView) findViewById(R.id.chat_room_chat_lv);
        messageEt = (EditText) findViewById(R.id.chat_room_message_et);
        sendBtn = (Button) findViewById(R.id.chat_room_send_btn);
        sendBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        // connectService.SendMessageToServer(messageEt.getText().toString(),0);
                    }
                }).start();
                messageEt.setText("");
                
            }
        });

        ChatRoomAdapter adapter = new ChatRoomAdapter(this, new ArrayList<Message>());
        chatLv.setAdapter(adapter);
    }

}
